package com.ky.manage.activity;

import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ky.manage.BaseApplication;
import com.ky.manage.R;
import com.ky.manage.activity.indoor.IndoorSecurityCheckAddDetailActivity;
import com.ky.manage.base.BaseActivity;
import com.ky.manage.model.req.IndoorSecureAddReq;
import com.ky.manage.model.response.ClientInfoAddResp;
import com.ky.manage.utils.AddressRegionSelectUtils;
import com.ky.manage.utils.ToastUtils;
import com.ky.manage.utils.ZyUtils;
import com.ky.manage.utils.http.AsyncHttpUtils;
import com.ky.manage.utils.http.BusinessHttpReqUtils;
import com.loopj.android.http.TextHttpResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

/**
 * 客户信息编辑页
 */
public class ClientInfoEditActivity extends BaseActivity {

    //必填
    private EditText userNameEt, userPhoneEt, detailAddressEt, brandEt;
    private TextView regionSelectTv;

    //选填
    private EditText idCardEt, emailEt, biLuBrandTypeEt;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_client_info_edit;
    }

    @Override
    protected void initView() {
        findView(R.id.back_iv, v -> {
            finish();
        });
        findTextView(R.id.page_title_tv, "客户信息");
        findView(R.id.create_check_log, v -> {
            //下一步，检修记录
            if (ZyUtils.getInstance().isNetConnected()) {
                createNewCheckLog();
            }
        });
        userNameEt = findView(R.id.user_name_et);
        userPhoneEt = findView(R.id.user_phone_et);
        brandEt = findView(R.id.brand_et);
        detailAddressEt = findView(R.id.detail_address_et);
        regionSelectTv = findView(R.id.region_tv, v -> {
            AddressRegionSelectUtils.getInstance().showRegionSelect(this, new AddressRegionSelectUtils.OnRegionSelect() {
                @Override
                public void onSelect(String result) {
                    regionSelectTv.setText(result);
                }

                @Override
                public void onCancel() {

                }
            });
        });
        idCardEt = findView(R.id.id_card_et);
        emailEt = findView(R.id.email_et);
        biLuBrandTypeEt = findView(R.id.bi_lu_brand_et);
    }

    private void createNewCheckLog() {
        IndoorSecureAddReq addReq = new IndoorSecureAddReq();
        addReq.clientName = userNameEt.getText().toString().trim();
        if (TextUtils.isEmpty(addReq.clientName)) {
            ToastUtils.showRoundRectToast("请输入客户姓名");
            return;
        }
        addReq.clientTel = userPhoneEt.getText().toString().trim();
        if (TextUtils.isEmpty(addReq.clientTel)) {
            ToastUtils.showRoundRectToast("请输入客户联系电话");
            return;
        }
        addReq.brandName = brandEt.getText().toString().trim();
        if (TextUtils.isEmpty(addReq.brandName)) {
            ToastUtils.showRoundRectToast("请输入品牌");
            return;
        }
        addReq.address = regionSelectTv.getText().toString().replaceAll("/", ",").replaceAll(" ", "").trim();
        addReq.addressList = regionSelectTv.getText().toString().replaceAll("/", ",").replaceAll(" ", "").trim();;
        if (TextUtils.isEmpty(addReq.address)) {
            ToastUtils.showRoundRectToast("请选择区域");
            return;
        }
        addReq.detailedAddress = detailAddressEt.getText().toString();
        if (TextUtils.isEmpty(addReq.detailedAddress)) {
            ToastUtils.showRoundRectToast("请输入详细地址");
            return;
        }
        ZyUtils.getInstance().showLoadingDialog(this, "");
        addReq.checkPersonId = BaseApplication.getLoginUserInfo().userId;
        addReq.numberId = idCardEt.getText().toString().trim();
        addReq.email = emailEt.getText().toString().trim();
        addReq.furnaceCode = biLuBrandTypeEt.getText().toString().trim();

        JSONObject pubReq = null;
        try {
            String reqJson = new Gson().toJson(addReq);
            pubReq = new JSONObject(reqJson);
        } catch (JSONException e) {
            Log.d("createNewCheckLog", "e = " + e.toString());
        }
        AsyncHttpUtils.getInstance().postJson(this,BusinessHttpReqUtils.SERVICE_DOMAIN + "/appApi/indoorAdd", pubReq, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                ZyUtils.getInstance().insertLog("createNewCheckLog", "onFailure statusCode = " + statusCode
                        + ", responseString = " + responseString);
                ZyUtils.getInstance().hideLoadingDialog();
                ToastUtils.showRoundRectToast("接口异常，请重试");
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                ZyUtils.getInstance().insertLog("createNewCheckLog", "onSuccess statusCode = " + statusCode
                        + ", responseString = " + responseString);
                ZyUtils.getInstance().hideLoadingDialog();
                if (statusCode == 200) {
                    ClientInfoAddResp clientInfoAddResp = BusinessHttpReqUtils.getInstance().parseJsonObj(responseString, ClientInfoAddResp.class);
                    if (null != clientInfoAddResp && clientInfoAddResp.code == 200) {
                        IndoorSecurityCheckAddDetailActivity.toSecurityCheckAddDetailActivity(mCurActivity, clientInfoAddResp.data);
                    } else {
                        ToastUtils.showRoundRectToast("接口异常，请重试");
                    }
                } else {
                    ToastUtils.showRoundRectToast("接口异常，请重试");
                }
            }
        });
    }

    @Override
    protected void initData() {

    }
}